home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
tex
/
tgrind.zip
/
TFONTEDP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-06-29
|
17KB
|
639 lines
#ifndef lint
static char sccsid[] = "@(#)@(#)tfontedpr.c 1.3 (LBL) 4/12/85";
#endif
/* tfontedpr - general purpose "pretty printer" for use with TeX.
*
* Copyright (C) 1985 by Van Jacobson, Lawrence Berkeley Laboratory.
* This program may be freely used and copied but may not be sold
* without the author's written permission. This notice must remain
* in any copy or derivative.
*
* This program is used as part of the "tgrind" shell script. It
* converts program source file(s) to TeX input files.
*
* This program is an adaptation of "vfontedpr" v4.2 (12/11/84) from
* the 4.2bsd Unix distribution. Vfontedpr was written by Dave
* Presotto (based on an earlier program of the same name written by
* Bill Joy).
*
* I would welcome comments, enhancements, bug fixes, etc. Please
* mail them to:
* van@lbl-rtsg.arpa (from arpanet, milnet, csnet, etc.)
* ..!ucbvax!lbl-csam!van (from Usenet/UUCP)
*
* Modifications.
* --------------
* 29Jun90 Jim Van Zandt: Appending "\headline={\hfil}" rather than
* "\end" to output file, so it can be included in
* another TeX file. Enclosing the output in an
* extra set of of braces for the same reason.
* 25May90 Jim Van Zandt: Allowing for tabwidth other than 8 (e.g., -t4
* switch would set tabwidth to four columns). Added some
* commented braces so the editor's brace matching works.
* 30Mar85 Chris & Van: Fixed "\C" & "\S" (comment & string start indicators)
* to really appear at the start of comments & strings.
* Changes for speeded-up expmatch.
* 29Mar85 Chris Torek (chris@maryland): Bug fixes for '~' and '^L'
* output. Most cpu-time eaters recoded to improve
* efficiency.
* 10Feb85 Van Written.
*/
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define boolean int
#define TRUE 1
#define FALSE 0
#define NIL 0
#define STANDARD 0
#define ALTERNATE 1
#define STRLEN 10 /* length of strings introducing things */
#define PNAMELEN 80 /* length of a function/procedure name */
#define PSMAX 20 /* size of procedure name stacking */
/* regular expression routines */
char *expmatch(); /* match a string to an expression */
char *convexp(); /* convert expression to internal form */
char *tgetstr();
boolean isproc();
char *ctime();
/*
* The state variables
*/
boolean incomm; /* in a comment of the primary type */
boolean instr; /* in a string constant */
boolean inchr; /* in a string constant */
boolean nokeyw = FALSE; /* no keywords being flagged */
boolean prccont; /* continue last procedure */
int comtype; /* type of comment */
int psptr; /* the stack index of the current procedure */
char pstack[PSMAX][PNAMELEN+1]; /* the procedure name stack */
int plstack[PSMAX]; /* the procedure nesting level stack */
int blklevel; /* current nesting level */
char *defsfile = DEFSFILE; /* name of language definitions file */
char pname[BUFSIZ+1];
int tabwidth = 8; /* assumed width of a tab character */
/*
* The language specific globals
*/
char *language = "c"; /* the language indicator */
char *l_keywds[BUFSIZ/2]; /* keyword table address */
char *l_prcbeg; /* regular expr for procedure begin */
char *l_combeg; /* string introducing a comment */
char *l_comend; /* string ending a comment */
char *l_acmbeg; /* string introducing a comment */
char *l_acmend; /* string ending a comment */
char *l_blkbeg; /* string begining of a block */
char *l_blkend; /* string ending a block */
char *l_strbeg; /* delimiter for string constant */
char *l_strend; /* delimiter for string constant */
char *l_chrbeg; /* delimiter for character constant */
char *l_chrend; /* delimiter for character constant */
char l_escape; /* character used to escape characters */
boolean l_toplex; /* procedures only defined at top lex level */
boolean l_onecase; /* upper & lower case equivalent */
/*
* global variables also used by expmatch
*/
extern boolean _escaped; /* if last character was an escape */
extern char *_start; /* start of the current string */
int (*re_strncmp)(); /* function to do string compares */
extern int strncmp();
extern int lc_strncmp();
/*
* The following table converts ASCII characters to a printed
* representation, taking care of all the TeX quoting. N.B.: all
* single-character strings are assumed to be equivalent to the
* character for that index (i.e., printtab['c'] can't be "f").
* (This is purely for efficiency hacking.)
*/
char *printtab[128] = {
"\0x", "\\^A", "\\^B", "\\^C", "\\^D", "\\^E", "\\^F", "\\^G",
"\\^H", "\t", "}}\n", "\\^K", "\0x", "\\^M", "\\^N", "\\^O",
"\\^P", "\\^Q", "\\^R", "\\^S", "\\^T", "\\^U", "\\^V", "\\^W",
"\\^X", "\\^Y", "\\^Z", "\\^[", "\\^\\!","\\^]", "\\^\\^","\\^_",
" ", "!", "\\\"", "\\#", "\\$", "\\%", "\\&", "\\'",
"(", ")", "*", "+", ",", "\\-", ".", "\\/",
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", ":", ";", "\\<", "=", "\\>", "?",
"@", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "[", "\\!", "]", "\\^", "\\_",
"`", "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z", "\\{", "\\|", "\\}", "\\~", "\\^?",
};
/* Output a character, with translation. Avoid side effects with this
macro! */
#define outchar(c) (printtab[c][1] ? printf("%s", printtab[c]) : putchar(c))
/*
* Output a TeX command to tab to column "col" (see tgrindmac.tex for a
* partial explanation of the bizarre brace arrangement).
*/
#define tabto(col) printf("}\\Tab{%d}{", col);
main(argc, argv)
int argc;
register char *argv[];
{
char *fname = "", *p;
struct stat stbuf;
char buf[BUFSIZ];
char strings[2 * BUFSIZ];
char defs[2 * BUFSIZ];
#ifdef __TURBOC__ /* "tgrindmac" -> "tgrind" for PC's short filenames */
printf("\\input tgrind\n{\n");
#else
printf("\\input tgrindmac\n{\n");
#endif
argc--, argv++;
do {
register char *cp;
register int i;
if (argc > 0) {
if (!strcmp(argv[0], "-h")) {
if (argc == 1) {
printf("\\Head{}\n");
argc = 0;
goto rest;
}
printf("\\Head{");
putstr( argv[1] );
printf( "}\n" );
argc--, argv++;
argc--, argv++;
if (argc > 0)
continue;
goto rest;
}
/* take input from the standard place */
if (!strcmp(argv[0], "-")) {
argc = 0;
goto rest;
}
/* indicate no keywords */
if (!strcmp(argv[0], "-n")) {
nokeyw++;
argc--, argv++;
continue;
}
/* specify the language */
if (!strncmp(argv[0], "-l", 2)) {
language = argv[0]+2;
argc--, argv++;
continue;
}
/* specify the language description file */
if (!strncmp(argv[0], "-d", 2)) {
defsfile = argv[1];
argc--, argv++;
argc--, argv++;
continue;
}
/* set the tabwidth */
if (!strncmp(argv[0], "-t", 2)) {
tabwidth = atoi(argv[0]+2);
if(tabwidth < 1 || tabwidth > 64) tabwidth = 8;
argc--, argv++;
continue;
}
/* open the file for input */
if (freopen(argv[0], "r", stdin) == NULL) {
perror(argv[0]);
exit(1);
}
fname = argv[0];
argc--, argv++;
}
rest:
/*
* get the language definition from the defs file
*/
i = tgetent (defs, language, defsfile);
if (i == 0) {
fprintf (stderr, "no entry for language %s\n", language);
exit (0);
} else if (i < 0) {
fprintf (stderr, "cannot find vgrindefs file %s\n", defsfile);
exit (0);
}
p = strin